#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：turbo 
@File    ：init.py
@Author  ：赵加响
@Date    ：2023/5/22 11:33 
@Desc    ：使用init命令初始化DB，这些DB为系统运行的基础
"""
from django.core.management import BaseCommand

from apps.accounts.models import SystemSettings, User
from apps.codetable.models import CodeTableHead, CodeTableDetail
from apps.rbac.models import Operate, Menu, MenuOperate, Role
from turbo import settings
from utils.system_settings.constant import PASSWORD_VALID_DAYS, PASSWORD_CHANGE_REMINDER_DAYS, INITIAL_PASSWORD, \
    DISABLE_PASSWORD_DUPLICATION_TIMES, PASSWORD_REGEX


class Command(BaseCommand):
    help = 'Initialize DB Data'

    def handle(self, *args, **options):
        self.stdout.write(self.style.SUCCESS('Initialize System Settings DB.'))
        SystemSettings.objects.get_or_create(code=PASSWORD_VALID_DAYS,
                                             defaults={'value': '365', 'content': '密码有效天数', 'value_type': 'int'})
        SystemSettings.objects.get_or_create(code=PASSWORD_CHANGE_REMINDER_DAYS,
                                             defaults={'value': '7', 'content': '密码修改提醒天数',
                                                       'value_type': 'int'})
        SystemSettings.objects.get_or_create(code=INITIAL_PASSWORD,
                                             defaults={'value': '123456', 'content': '默认初始密码',
                                                       'value_type': 'str'})
        SystemSettings.objects.get_or_create(code=DISABLE_PASSWORD_DUPLICATION_TIMES,
                                             defaults={'value': '', 'content': '禁止密码重复次数', 'value_type': 'int'})
        SystemSettings.objects.get_or_create(code=PASSWORD_REGEX,
                                             defaults={
                                                 'value': '',
                                                 'content': '密码规则正则表达式', 'value_type': 'str'})

        self.stdout.write(self.style.SUCCESS('Initialize Operates DB.'))
        inquiry, created = Operate.objects.get_or_create(code='INQUIRY', defaults={'name': '查询', 'name_en': 'Inquiry'})
        create, created = Operate.objects.get_or_create(code='CREATE', defaults={'name': '新建', 'name_en': 'Create'})
        update, created = Operate.objects.get_or_create(code='UPDATE', defaults={'name': '更新', 'name_en': 'Update'})
        delete, created = Operate.objects.get_or_create(code='DELETE', defaults={'name': '删除', 'name_en': 'Delete'})
        authorize, created = Operate.objects.get_or_create(code='AUTHORIZE',
                                                           defaults={'name': '授权', 'name_en': 'Authorize'})
        assigning, created = Operate.objects.get_or_create(code='ASSIGNING',
                                                           defaults={'name': '分配', 'name_en': 'Assigning'})
        reset, created = Operate.objects.get_or_create(code='RESET', defaults={'name': '数据重置', 'name_en': 'Reset'})
        release, created = Operate.objects.get_or_create(code='RELEASE', defaults={'name': '生效', 'name_en': 'Release'})
        close, created = Operate.objects.get_or_create(code='CLOSE', defaults={'name': '关闭', 'name_en': 'Close'})
        download, created = Operate.objects.get_or_create(code='DOWNLOAD',
                                                          defaults={'name': '下载', 'name_en': 'Download'})
        upload, created = Operate.objects.get_or_create(code='UPLOAD',
                                                        defaults={'name': '上传', 'name_en': 'Upload'})

        self.stdout.write(self.style.SUCCESS('Initialize Menus DB.'))
        # 主页设定为失效状态
        Menu.objects.get_or_create(code='HOME-1000',
                                   defaults={'name': '主页', 'name_en': 'Home', 'content': '前端主页菜单',
                                             'is_active': False})
        rbac, created = Menu.objects.get_or_create(code='RBAC-1000',
                                                   defaults={'name': '权限管理', 'name_en': 'Permission',
                                                             'content': '前端权限管理'})
        account, created = Menu.objects.get_or_create(code='RBAC-1010',
                                                      defaults={'name': '用户管理', 'name_en': 'User Management',
                                                                'content': '前端用户管理', 'parent': rbac})

        role, created = Menu.objects.get_or_create(code='RBAC-1020',
                                                   defaults={'name': '角色管理', 'name_en': 'Role Management',
                                                             'content': '前端角色管理', 'parent': rbac})

        system, created = Menu.objects.get_or_create(code='SYSTEM-1000',
                                                     defaults={'name': '系统', 'name_en': 'System Setting',
                                                               'content': '前端系统配置'})
        parameter, created = Menu.objects.get_or_create(code='SYSTEM-1010',
                                                        defaults={'name': '系统参数', 'name_en': 'System Parameters',
                                                                  'content': '前端系统参数', 'parent': system})
        logs, created = Menu.objects.get_or_create(code='SYSTEM-1020',
                                                   defaults={'name': '日志', 'name_en': 'Logs',
                                                             'content': '前端日志', 'parent': system})

        self.stdout.write(self.style.SUCCESS('Initialize MenuOperates DB.'))

        user_inquiry, created = MenuOperate.objects.get_or_create(menu=account, operate=inquiry)
        user_create, created = MenuOperate.objects.get_or_create(menu=account, operate=create)
        user_update, created = MenuOperate.objects.get_or_create(menu=account, operate=update)
        user_delete, created = MenuOperate.objects.get_or_create(menu=account, operate=delete)
        user_reset, created = MenuOperate.objects.get_or_create(menu=account, operate=reset)
        role_inquiry, created = MenuOperate.objects.get_or_create(menu=role, operate=inquiry)
        role_create, created = MenuOperate.objects.get_or_create(menu=role, operate=create)
        role_update, created = MenuOperate.objects.get_or_create(menu=role, operate=update)
        role_delete, created = MenuOperate.objects.get_or_create(menu=role, operate=delete)
        role_authorize, created = MenuOperate.objects.get_or_create(menu=role, operate=authorize)
        role_assigning, created = MenuOperate.objects.get_or_create(menu=role, operate=assigning)
        parameter_inquiry, created = MenuOperate.objects.get_or_create(menu=parameter, operate=inquiry)
        parameter_update, created = MenuOperate.objects.get_or_create(menu=parameter, operate=update)
        logs_inquiry, created = MenuOperate.objects.get_or_create(menu=logs, operate=inquiry)
        logs_delete, created = MenuOperate.objects.get_or_create(menu=logs, operate=delete)
        logs_download, created = MenuOperate.objects.get_or_create(menu=logs, operate=download)

        user_inquiry.menu_operate_urls.get_or_create(description='用户 - 查询', url='/account/list/', method='GET')
        user_create.menu_operate_urls.get_or_create(description='用户 - 创建', url='/account/list/', method='POST')
        user_update.menu_operate_urls.get_or_create(description='用户 - 更新', url='/account/list/', method='PUT')
        user_delete.menu_operate_urls.get_or_create(description='用户 - 删除', url='/account/list/', method='DELETE')
        user_reset.menu_operate_urls.get_or_create(description='用户 - 密码重置', url='/account/reset_password/',
                                                   method='POST')
        role_inquiry.menu_operate_urls.get_or_create(description='角色 - 查询', url='/rbac/role/', method='GET')
        role_create.menu_operate_urls.get_or_create(description='角色 - 创建', url='/rbac/role/', method='POST')
        role_update.menu_operate_urls.get_or_create(description='角色 - 更新', url='/rbac/role/', method='PUT')
        role_delete.menu_operate_urls.get_or_create(description='角色 - 删除', url='/rbac/role/', method='DELETE')
        role_authorize.menu_operate_urls.get_or_create(description='角色 - 菜单授权',
                                                       url='/rbac/role_menu_authorize/', method='POST')
        role_assigning.menu_operate_urls.get_or_create(description='角色 - 分配用户', url='/rbac/role_assign_user/',
                                                       method='POST')
        parameter_inquiry.menu_operate_urls.get_or_create(description='系统参数 - 查询',
                                                          url='/system_settings/list/', method='GET')
        parameter_update.menu_operate_urls.get_or_create(description='系统参数 - 更新', url='/system_settings/list/',
                                                         method='PUT')
        logs_inquiry.menu_operate_urls.get_or_create(description='日志 - 文件',
                                                     url='/system_settings/logs_file_handles/', method='GET')
        logs_inquiry.menu_operate_urls.get_or_create(description='日志 - 目录',
                                                     url='/system_settings/logs_directory_structure/', method='GET')
        logs_delete.menu_operate_urls.get_or_create(description='日志 - 删除文件',
                                                    url='/system_settings/delete_log_file/', method='POST')
        logs_delete.menu_operate_urls.get_or_create(description='日志 - 删除目录',
                                                    url='/system_settings/delete_log_folder/', method='POST')
        logs_download.menu_operate_urls.get_or_create(description='日志 - 下载',
                                                      url='/system_settings/download_log_file/', method='GET')

        self.stdout.write(self.style.SUCCESS('Initialize Roles DB.'))
        role, created = Role.objects.get_or_create(code='admin',
                                                   defaults={'name_zh': '管理员', 'name_en': 'Administrator',
                                                             'is_admin': True})
        if created:
            self.stdout.write(self.style.SUCCESS('Create role (code: admin, administrator: true)'))

        self.stdout.write(self.style.SUCCESS('Initialize Users DB.'))
        user, created = User.objects.get_or_create(username='admin',
                                                   defaults={'name': '管理员', 'is_superuser': True, 'is_staff': True,
                                                             'is_active': True, 'init_status': False,
                                                             'user_type': 'A', 'invalid_date': '2049-12-31'})

        if created:
            user.roles.set(Role.objects.filter(code__exact='admin'))
            user.set_password('123456')
            user.save()
            self.stdout.write(
                self.style.SUCCESS('Create user (username: admin, password: 123456, role: admin, superuser: true)'))
